package com.yingxin.yxerp.module.organization.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.taobao.api.ApiException;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.encrpty.pwd.Sha1Encrpty;
import com.yingxin.yx.framework.encrpty.rsa.RsaUtil;
import com.yingxin.yx.framework.security.pwd.Sha1PasswordEncoder;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yx.framework.user.dto.AppPermissionDto;
import com.yingxin.yx.framework.user.dto.PermissionDto;
import com.yingxin.yxerp.module.attachment.constant.AttachConst;
import com.yingxin.yxerp.module.attachment.service.IAttachmentService;
import com.yingxin.yxerp.module.oplog.aop.OplogAnnotation;
import com.yingxin.yxerp.module.organization.entity.User;
import com.yingxin.yxerp.module.organization.entity.enums.AccountStatus;
import com.yingxin.yxerp.module.organization.entity.enums.StaffStatus;
import com.yingxin.yxerp.module.organization.mapper.UserMapper;
import com.yingxin.yxerp.module.organization.service.IDepartmentService;
import com.yingxin.yxerp.module.organization.service.IUserAppRoleService;
import com.yingxin.yxerp.module.organization.service.IUserRoleService;
import com.yingxin.yxerp.module.organization.service.IUserService;
import com.yingxin.yxerp.module.permission.entity.AppRole;
import com.yingxin.yxerp.module.permission.entity.Role;
import com.yingxin.yxerp.module.permission.mapper.AppPermissionMapper;
import com.yingxin.yxerp.module.permission.mapper.PermissionMapper;
import com.yingxin.yxerp.module.permission.service.IAppRoleService;
import com.yingxin.yxerp.module.permission.service.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 用户 服务实现类
 * @author fanchao
 * @since 2021-05-13
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    @SuppressWarnings("all")
    private PermissionMapper permissionMapper;

    @Autowired
    private AppPermissionMapper appPermissionMapper;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IAppRoleService appRoleService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IUserAppRoleService userAppRoleService;

    @Autowired
    private IUserSupport userSupport;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IAttachmentService attachmentService;


    /**
     * 获取 mapper
     */
    @Override
    public UserMapper mapper(){
        return baseMapper;
    }

    @Override
    public List<User> listByIds(Set<String> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().in(User::getId, ids);
        return list(qw);
    }

    @Override
    public List<User> listByIds(List<String> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().in(User::getId, ids);
        return list(qw);
    }

    @Override
    public String getStaffNameById(String id) {
        User user = getById(id);
        if (user == null) {
            return "";
        } else {
            return user.getStaffName();
        }
    }


    /**
     * 根据 deptId 查询
     */
    @Override
    public List<User> listByDeptId(String deptId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getDeptId, deptId);
        return list(qw);
    }

    /**
     * 根据 deptId 查询
     * @param deptId
     * @return
     */
    @Override
    public List<User> listByDeptIds(String deptId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getDelFlag,CommonConst.FALSE);
        qw.lambda().eq(User::getAccountStatus,CommonConst.TRUE);
        qw.lambda().eq(User::getDeptId, deptId);
        return list(qw);
    }

    /**
     * 部门员工计数
     */
    @Override
    public Integer countByDeptId(String deptId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getDeptId, deptId);
        qw.lambda().ne(User::getStaffStatus, StaffStatus.LZ);
        return count(qw);
    }

    /**
     * 根据用户名查询
     */
    @Override
    public User getByUsername(String username) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda().eq(User::getUsername, username);
        return getOne(qw);
    }

    /**
     * 根据电话查询
     */
    @Override
    public User getByPhone(String phone) {
        return null;
    }

    @Override
    public User getByWechatOpenId(String openId) {
        return null;
    }


    /**
     * 微信绑定
     */
    @Override
    public void bindWechat(String userId, String openId, String nickname) {

    }

    /**
     * 微信绑定2
     */
    @Override
    public void bindWechatInfo(String userId, String openId, String nickname, String wechatAvatarUrl){

    }

    /**
     * 解绑微信
     */
    @Override
    public Boolean unboundWechat() {
        return null;
    }

    /**
     * 当前最大工号
     */
    public Integer getMaxStaffNo() {
        Integer maxStaffNo = baseMapper.getMaxStaffNo();
        if (maxStaffNo < 1000) {
            maxStaffNo = 1000;
        }
        return maxStaffNo;
    }

    /**
     * 创建用户
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "新增", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(User user) {
        user.setStaffNo(user.getStaffNo() == null ? getMaxStaffNo() + 1 : user.getStaffNo());
        // 密码
        String password = RsaUtil.decryptByPrivateKey(user.getPassword().trim());
        user.setPassword(Sha1Encrpty.encryptPassword(password));
        // 默认开启
        user.setAccountStatus(AccountStatus.ENABLED);
        user.setStaffStatus("ZZ");
        user.setAvatar(user.getAvatarAttachment() == null ? user.getAvatar() : user.getAvatarAttachment().getAttachmentPath());
        save(user);
        if (StringUtils.isNotEmpty(user.getDeptId())) {
            departmentService.updateDirectStaffCountByDeptId(user.getDeptId());
        }
    }

    /**
     * 修改用户
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "修改", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void update(User user) {
        User userById = getById(user.getId());
        userById.setUsername(user.getUsername());
        userById.setDeptId(user.getDeptId());
        userById.setStaffName(user.getStaffName());
        userById.setAccountStatus(user.getAccountStatus());
        updateById(userById);
        // 更新部门人数
        departmentService.updateDirectStaffCountByDeptId(user.getDeptId());
        // 附件
        attachmentService.addInfo(user.getId(), BusinessType.USER, AttachConst.AVATAR, user.getAvatarAttachment(), Boolean.TRUE);
    }

    /**
     * 同步用户
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "修改", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void synchronousUpdate(User user) {

    }

    /**
     * 修改用户
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "修改用户头像", desc = "用户修改头像")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void updateAvatar(User user) {
        attachmentService.addInfo(user.getId(), BusinessType.USER, AttachConst.AVATAR, user.getAvatarAttachment(), Boolean.TRUE);
    }

    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "管理员编辑", desc = "管理员修改用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void userEdit(User user, String username, String password, String accountRemarks) {
        String newPassword = RsaUtil.decryptByPrivateKey(password.trim());
        user.setPassword(Sha1Encrpty.encryptPassword(newPassword));
        user.setUsername(username);
        updateById(user);
        userSupport.kickOutUser(user.getId());
    }

    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "账户启用", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void enable(User user) {
        if (user != null) {
            if (StringUtils.isBlank(user.getUsername())) {
                throw new BizException("用户未设置账户名");
            }
            if (StaffStatus.LZ.equals(user.getStaffStatus())) {
                throw new BizException("用户已经离职");
            }
            UpdateWrapper<User> uw = new UpdateWrapper<>();
            uw.lambda().set(User::getAccountStatus, AccountStatus.ENABLED);
            uw.lambda().eq(User::getId, user.getId());
            boolean success = update(uw);
            if (!success) {
                throw new BizException("操作失败");
            }
        }
    }

    /**
     * 禁用用户
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "账户禁用", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void disable(User user) {
        UpdateWrapper<User> uw = new UpdateWrapper<>();
        uw.lambda().set(User::getAccountStatus, AccountStatus.DISABLED);
        uw.lambda().eq(User::getId, user.getId());
        boolean success = update(uw);
        if (!success) {
            throw new BizException("操作失败");
        }
        // 踢出登录
        userSupport.kickOutUser(user.getId());
    }

    /**
     * 锁定
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void lock(String username) {
        User user = getByUsername(username);
        if (user != null) {
            UpdateWrapper<User> uw = new UpdateWrapper<>();
            uw.lambda().set(User::getLockFlag, CommonConst.TRUE);
            uw.lambda().eq(User::getId, user.getId());
            update(uw);
        }
    }

    /**
     * 解锁
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void unlock(User user) {
        UpdateWrapper<User> uw = new UpdateWrapper<>();
        uw.lambda().set(User::getLockFlag, CommonConst.FALSE);
        uw.lambda().eq(User::getId, user.getId());
        update(uw);
        // 删除 login_failure记录
        redisTemplate.opsForHash().delete("login_failure", user.getUsername());
    }

    /**
     * 删除用户
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "账户删除", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void delete(User user) {
        if (!AccountStatus.DISABLED.equals(user.getAccountStatus())) {
            throw new BizException("请先停用账户");
        }
        removeById(user.getId());
        userRoleService.deleteByUserId(user);
        if (StringUtils.isNotEmpty(user.getDeptId())) {
            departmentService.updateDirectStaffCountByDeptId(user.getDeptId());
        }
    }

    /**
     * 添加角色
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "设置角色", desc = "用户[{{user.staffName}} {{user.staffNo}}]，角色{{user.roleDesc}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void addRoles(String userId, List<String> roleIds) {
        User user = getById(userId);
        userRoleService.deleteByUserId(user);
        StringBuilder roleDesc = new StringBuilder();
        if (CollUtil.isNotEmpty(roleIds)) {
            List<Role> roles = roleService.listByIds(roleIds);
            for (Role role : roles) {
                userRoleService.create(user, role);
                roleDesc.append("[").append(role.getName()).append("] ");
            }
        }
        // 用户
        user.setRoleDesc(roleDesc.toString());
        updateById(user);
        // 激活权限加载
        userSupport.activateReloadPermissionsByUser(userId);
    }

    /**
     * app添加角色
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "设置角色", desc = "用户[{{user.staffName}} {{user.staffNo}}]，角色{{user.roleDesc}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void addAppRoles(String userId, List<String> roleIds) {
        User user = getById(userId);
        userAppRoleService.deleteByUserId(user);
        StringBuilder roleDesc = new StringBuilder();
        if (CollUtil.isNotEmpty(roleIds)) {
            List<AppRole> roles = appRoleService.listByIds(roleIds);
            for (AppRole role : roles) {
                userAppRoleService.create(user, role);
                roleDesc.append("[").append(role.getName()).append("] ");
            }
        }
        // 用户
        user.setAppRoleDesc(roleDesc.toString());
       // updateById(user);
        // 激活权限加载
       // userSupport.activateReloadPermissionsByUser(userId);
    }

    @Override
    public void saveHomeModules(String userId, List<String> modules) {
        User user = getById(userId);
        if (CollectionUtil.isNotEmpty(modules)) {
            Set<String> set = new HashSet<>(modules);
            user.setHomeWorkbenchModule(StringUtils.join(set, ","));
        } else {
            user.setHomeWorkbenchModule("");
        }
        updateById(user);
    }

    /**
     * 根据用户id获取权限
     */
    @Override
    public List<PermissionDto> listPermissionDtoByUserId(String userId) {
        List<PermissionDto> permissionList;
        if ("1".equals(userId)) {
            permissionList = permissionMapper.listDto();
        } else {
            permissionList = baseMapper.listPermissionDtoByUserId(userId);
        }
        return permissionList;
    }

    @Override
    public List<AppPermissionDto> listAppPermissionDtoByUserId(String userId) {
        List<AppPermissionDto> permissionList;
        if ("1".equals(userId)) {
            permissionList = appPermissionMapper.listAppPermissionDto();
        } else {
            permissionList = baseMapper.listAppPermissionDtoByUserId(userId);
        }
        return permissionList;
    }

    /**
     * 检查用户名是否重复
     */
    public void checkUsername(String userId, String username) {
        if (StringUtils.isNumeric(username)) {
            throw new BizException("账户名请用字母和数字组成");
        }
        User user = getByUsername(username);
        if (user != null) {
            if (!user.getId().equals(userId)) {
                throw new BizException("账户名重复");
            }
        }
        // 不能改成 name = id
        user = getById(username);
        if (user != null) {
            throw new BizException("账户名不合法");
        }
    }

    /**
     * pc修改密码
     * @param user 用户对象
     * @param password 新密码
     * @param confirmPassword 确认密码
     * @param oldPassword 旧密码
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "修改密码", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void modifyPassword(User user, String password, String confirmPassword, String oldPassword) {
        String newPassword = RsaUtil.decryptByPrivateKey(password.trim());
        String confirmNewPassword = RsaUtil.decryptByPrivateKey(confirmPassword.trim());
        if (!newPassword.equals(confirmNewPassword)) {
            throw new BizException("两次密码输入不一致");
        }
        //
        user = getById(userSupport.getUserId());
        if (!new Sha1PasswordEncoder().matches(RsaUtil.decryptByPrivateKey(oldPassword.trim()), user.getPassword())) {
            throw new BizException("旧密码错误");
        }
        if( new Sha1PasswordEncoder().matches(RsaUtil.decryptByPrivateKey(password.trim()), user.getPassword() ) ){
            throw new BizException("改后密码与上次密码不能一致！");
        }
        user.setPassword(Sha1Encrpty.encryptPassword(newPassword));
        updateById(user);
    }

    /**
     * app修改密码
     * @param user 用户对象
     * @param password 新密码
     * @param confirmPassword 确认密码
     * @param oldPassword 旧密码
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "修改密码", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void appModifyPassword(User user, String password, String confirmPassword, String oldPassword) {
        String newPassword = RsaUtil.decryptByPrivateKey(password.trim());
        String confirmNewPassword = RsaUtil.decryptByPrivateKey(confirmPassword.trim());
        if (!newPassword.equals(confirmNewPassword)) {
            throw new BizException("两次密码输入不一致");
        }
        //
        updateById(user);
    }

    /**
     * 管理员修改PC密码
     * @param password 新密码
     * @param confirmPassword 确认密码
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "修改密码", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void adminPcModifyPassword(String userId,String password, String confirmPassword){
        String newPassword = RsaUtil.decryptByPrivateKey(password.trim());
        String confirmNewPassword = RsaUtil.decryptByPrivateKey(confirmPassword.trim());
        if (!newPassword.equals(confirmNewPassword)) {
            throw new BizException("两次密码输入不一致");
        }
        //
        User user = getById(userId);
        user.setPassword( Sha1Encrpty.encryptPassword(newPassword) );
        updateById(user);
    }

    /**
     * 管理员修改app密码
     * @param password 新密码
     * @param confirmPassword 确认密码
     */
    @OplogAnnotation(biz = BusinessType.USER, id = "{{user.id}}", action = "修改密码", desc = "用户[{{user.staffName}} {{user.staffNo}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void adminAppModifyPassword( String userId,String password, String confirmPassword){
        String newPassword = RsaUtil.decryptByPrivateKey(password.trim());
        String confirmNewPassword = RsaUtil.decryptByPrivateKey(confirmPassword.trim());
        if (!newPassword.equals(confirmNewPassword)) {
            throw new BizException("两次密码输入不一致");
        }
        //
        User user = getById(userId);
        updateById(user);
    }

    @Override
    public List<String> listHomeWorkbenchModulesByUserId(String userId) {
        List<String> modules = Lists.newArrayList();
        User user = getById(userId);
        if (null != user && StringUtils.isNotEmpty(user.getHomeWorkbenchModule())) {
            modules = Arrays.asList(user.getHomeWorkbenchModule().split(","));
        }
        return modules;
    }

    /**
     * 微信解绑
     */
    @Override
    public boolean wechatOnlineTranslation(String userId){
        return true;
    }

    /**
     * 开通和关闭App登录
     */
    @Override
    public void isOpenAppLogon(String userId){
        User user = getById( userId );

        String isAppLogon = "0";
        if (user.getIsAppLogon().trim().equals("0")){
            isAppLogon = "1";
        }else{
            isAppLogon = "0";
        }

        UpdateWrapper<User> uw = new UpdateWrapper<>();
        uw.lambda().set(User::getIsAppLogon, isAppLogon);
        uw.lambda().eq(User::getId, userId);
        update(uw);
    }

    /**
     * 同步员工档案 - 钉钉
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void synchronism() throws ApiException, IllegalAccessException {

    }

    /**
     * 同步员工档案 - 企业微信
     * 注意：先同步人事助手信息后同步通讯录，否则 sys_user.we_com_user_id 是空
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void synchronismWeCom() {

    }

    /**
     * 同步离职员工档案
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void departSynchronism() throws Exception {

    }

    /**
     * 查找 ding_user_id 是否存在
     * @param dingUserId
     * @return
     */
    @Override
    public List<User> checkIsDingUserId(String dingUserId){
        return null;
    }

    /**
     * 查找 we_com_user_id 是否存在
     * @param weComUserId
     * @return
     */
    @Override
    public List<User> checkIsWeComUserId(String weComUserId){
        return null;
    }

    /*
    @Override
    public List<Permission> listPermissionsByUserId(String userId) {
        List<Permission> permissionList;
        if ("1".equals(userId)) {
            permissionList = permissionService.list();
        } else {
            permissionList = baseMapper.listPermissionsByUserId(userId);
        }
        return permissionList;
    }
    */

     /*
    // 查询用户的角色
    List<UserRole> userRoleList = userRoleService.listByUserId(userId);
    List<String> roleIds = ListHelper.toList(UserRole::getRoleId, userRoleList);
    // 查询角色的权限
    List<RolePermission> rolePermissionList = rolePermissionService.listByRoleIds(roleIds);
    List<String> permissionIds = ListHelper.toList(RolePermission::getPermissionId, rolePermissionList);
    // 查询权限
    List<Permission> permissions = permissionService.listByPermissionIds(permissionIds);
    List<PermissionDto> permissionList = new ArrayList<>();
    for (Permission permission : permissions) {
        PermissionDto respVo = new PermissionDto();
        BeanUtil.copyProperties(permission, respVo);
        permissionList.add(respVo);
    }
    */

    /*
    Object obj = redisTemplate.opsForHash().get("menu", userId);
    if (obj != null) {
        try {
            permissionList = JSON.parseArray(obj.toString(), PermissionDto.class);
            return permissionList;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    redisTemplate.opsForHash().put("menu", userId, JSON.toJSONString(permissionList));
    */


    /**
     * 获取dinguserid 的员工，否则获取证件编号的员工
     */
    @Override
    public List<User> getDingUserIdOrIdCardNo(String dingUserId, String idCardNo){
        return null;
    }

    /**
     * 查看密码
     * @return
     */
    @Override
    public Map<String,String> viewPassword() {
        User user = getById(userSupport.getUserId());
        String password = new Sha1PasswordEncoder().encode(user.getPassword());
        //String AppPassword = new Sha1PasswordEncoder().encode(user.getAppPassword());
        Map<String, String> map = new HashMap<String, String>();
        map.put("password",password);
        //map.put("AppPassword",AppPassword);
        return map;
    }

    /**
     * 根据身份证号码获取
     */
    @Override
    public User getByIdCardNo( String idCardNo ){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq( User::getDelFlag, CommonConst.FALSE );

        List<String> staffStatus = new ArrayList<>();
        staffStatus.add( StaffStatus.SY );
        staffStatus.add( StaffStatus.ZZ );
        queryWrapper.lambda().in( User::getStaffStatus, staffStatus );
        //queryWrapper.lambda().eq( User::getIdCardNo, idCardNo );
        List<User> users = list( queryWrapper );
        if( CollectionUtil.isNotEmpty( users ) ){
            return users.get( users.size() - 1 );
        }
        return null;
    }

    /**
     * 创建流程验证，用户和部门
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> createVariables(String userId) {
        Map<String, Object> variables = new HashMap<>();
        User user = getById(userId);
        if (StringUtils.isNotBlank(user.getDeptId())) {
            variables.put("deptId", user.getDeptId());
        }
        variables.put("personId",userId);
        return variables;
    }

}
